package com.k2.gulimall.member.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.k2.common.vo.UserRegisterVo;
import com.k2.gulimall.member.entity.MemberEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.k2.gulimall.member.entity.MemberStatisticsInfoEntity;
import com.k2.gulimall.member.service.MemberStatisticsInfoService;
import com.k2.common.utils.PageUtils;
import com.k2.common.utils.R;



/**
 * 会员统计信息
 *
 * @author changchaojie
 * @email changchaojie@gmail.com
 * @date 2020-11-09 17:36:10
 */
@RestController
@RequestMapping("member/memberstatisticsinfo")
public class MemberStatisticsInfoController {
    @Autowired
    private MemberStatisticsInfoService memberStatisticsInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberStatisticsInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberStatisticsInfoEntity memberStatisticsInfo = memberStatisticsInfoService.getById(id);

        return R.ok().put("memberStatisticsInfo", memberStatisticsInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberStatisticsInfoEntity memberStatisticsInfo){
		memberStatisticsInfoService.save(memberStatisticsInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberStatisticsInfoEntity memberStatisticsInfo){
		memberStatisticsInfoService.updateById(memberStatisticsInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberStatisticsInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping(value = "/getMemberId",method = RequestMethod.POST)
    public R getMemberId(@RequestBody MemberStatisticsInfoEntity memberStatisticsInfo){
        List<MemberStatisticsInfoEntity> memberStatisticsInfoEntityList =
                memberStatisticsInfoService.getMemberIdToStatisticsInfo(memberStatisticsInfo);
        Map<String,Object> map = new HashMap<>();
        if (memberStatisticsInfoEntityList.size()>0){
            map.put("data",memberStatisticsInfoEntityList.get(0));
            map.put("code",1);
            map.put("msg","success");
            return R.ok(map);
        }else{
            return R.error(0,"该记录不存在");
        }
    }


    /**
     * 添加会员时，添加会员统计信息
     * @param userRegisterVo
     * @return
     */
    @PostMapping("/addmemberStatisticsInfo")
    public boolean addmemberStatisticsInfo(@RequestBody UserRegisterVo userRegisterVo){
        System.out.println(userRegisterVo);
        return memberStatisticsInfoService.addmemberStatisticsInfo(userRegisterVo);
    };

}
